﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WordLib.Model;
using WordService.Business.IBLL;
using WordService.Business;
using WordService.Utility;
using WordService.Model;

namespace WordLib.ImportUI
{
    public partial class FileReaderPanel : UserControl
    {
        public FileReaderPanel()
        {
            InitializeComponent();
            LoadForm();
        }

        private IFileBLL fileBLL = BLLManager.CreateFileBLL();
        private IArticleBLL articleBLL = BLLManager.CreateArticleBLL();
        private String filePath = String.Empty;

        private void LoadForm()
        {
            if (filePath.Equals(String.Empty))
            {
                this.btnLoadData.Enabled = false;
                this.chkIsComplete.Enabled = false;
                this.chkIsSame.Enabled = false;
            }
            else
            {
                this.btnLoadData.Enabled = true;
                this.chkIsComplete.Enabled = true;
                this.chkIsSame.Enabled = true;
            }
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            this.oldFileChooser.ShowDialog();
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            try
            {
                IList<Article> articles = ViewUtil.GetCreateModelList(((IList<InputDataModel>)this.dgvDataList.DataSource));
                this.articleBLL.CreateArticles(articles);
                MessageBox.Show(this, "您的数据已导入！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dgvDataList.DataSource = null;
                this.dgvDataList.Rows.Clear();
                filePath = String.Empty;
                LoadForm();
            }
            catch (Exception)
            {
                MessageBox.Show(this, "数据导入错误，请联系管理员！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 清除输出
        private void AddItem(String InputValue)
        {
            this.lbConsole.Items.Add(InputValue);
            this.lbConsole.Update();
            this.lbConsole.SelectedIndex = lbConsole.Items.Count - 1;
        }

        private void oldFileChooser_FileOk(object sender, CancelEventArgs e)
        {
            // 清除输出框
            this.lbConsole.Items.Clear();
            // 选择文件路径
            filePath = ((OpenFileDialog)sender).FileName;
            AddItem(@"你选择的文件为：");
            AddItem(filePath);
            FileInfo dataFile = new FileInfo(filePath);

            if (dataFile.Exists)
            {
                ExecuteLoad();
                AddItem(@"（总共" + this.dgvDataList.Rows.Count + "条数据）");
            }
        }

        private void chkIsSame_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkIsComplete.Checked)
            {
                AddItem(@"过滤重复数据……");
            }
            ExecuteLoad();
            AddItem(@"（最终读取总共" + this.dgvDataList.Rows.Count + "条数据）");
        }

        private void chkIsComplete_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkIsComplete.Checked)
            {
                AddItem(@"过滤不完整数据……");
            }
            ExecuteLoad();
            AddItem(@"（最终读取总共" + this.dgvDataList.Rows.Count + "条数据）");
        }

        private void ExecuteLoad()
        {
            IList<InputDataModel> dataList = ViewFilter.ModelListFilter(this.fileBLL.GetFileList(new FileInfo(filePath)), this.chkIsComplete.Checked, this.chkIsSame.Checked);
            this.dgvDataList.DataSource = dataList;
            this.dgvDataList.Update();
            LoadForm();
        }
    }
}
